package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.dtos.WmSensitivePageReqDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    /**
     * 新增敏感词
     *
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitiveDto dto) {
        //参数校验
        if (dto == null || StringUtils.isBlank(dto.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2.已存在的敏感词不可保存,存在的直接操作数据库
        WmSensitive wmSensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if (wmSensitive != null) {
            //存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }


        WmSensitive ws = new WmSensitive();
        ws.setSensitives(dto.getSensitives());
        ws.setCreatedTime(new Date());
        save(ws);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteSensitive(Integer id) {
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitiveDto dto) {
        //参数校验
        if (dto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }


        //敏感词已存在
        if (getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives())) != null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);

        }
        WmSensitive wmSensitive = getById(dto.getId());
        wmSensitive.setCreatedTime(new Date());
        wmSensitive.setSensitives(dto.getSensitives());
        updateById(wmSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmSensitivePageReqDto dto) {
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //数据处理
        //构建分页条件构造器
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery();

        //名城模糊查询
        wrapper.like(dto.getName() != null, WmSensitive::getSensitives, dto.getName());

        //创建时间倒叙
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        //查询数据库
        page(page, wrapper);

        //结果返回
        PageResponseResult res = new PageResponseResult((int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal());
        res.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        res.setData(page.getRecords());
        res.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());

        return res;
    }
}
